<template>
  <div class="loginBox">
    <el-form :model="ruleForm"
             status-icon
             ref="ruleForm"
             label-width="100px"
             class="demo-ruleForm"
             :rules="rules"
             size="small">
      <el-form-item class="username" label="用户名:" prop="username">
        <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item class="passwd" label="密码:" prop="passwd">
        <el-input type="password" v-model="ruleForm.passwd" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item class="confirmPasswd" label="确认密码:" prop="confirmPasswd">
        <el-input type="password" v-model="ruleForm.confirmPasswd" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    var validateUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'));
      } else {
        callback();
      }
    };
    var validatePasswd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.confirmPasswd !== '') {
          this.$refs.ruleForm.validateField('validateConfirmPasswd');
        }
        callback();
      }
    };
    var validateConfirmPasswd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.passwd) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: '',
        passwd: '',
        confirmPasswd: ''
      },
      rules: {
        username: [
          {validator: validateUsername, trigger: 'blur'}
        ],
        passwd: [
          {validator: validatePasswd, trigger: 'blur'}
        ],
        confirmPasswd: [
          {validator: validateConfirmPasswd, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$router.push('/home');
          this.$store.commit('loginUser/setUsername', this.$data.ruleForm.username)
          this.$store.commit('loginUser/setTimeOfLogin',{lastLoginTime:new Date(),loginTime:new Date()})
          window.localStorage.setItem('TOKEN', 'braea qwe27834$@#$6ZDF234')
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.loginBox {
  width: 100%;
  height: 100%;
  background-image: url("../assets/loginIMG.png");
  background-repeat: no-repeat;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.demo-ruleForm {
  height: auto;
  padding: 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-content: center;
  width: 380px;

}

.username, .passwd, .confirmPasswd {
  width: 200px;
}
</style>